package team.se.tms.controller.talent_training_program;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.enums.DateEnum;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.TalentTrainingProgramInfo;
import team.se.tms.service.TalentTrainingProgramInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("talentTrainingProgram")
public class TalentTrainingProgramInfoController extends BaseController{
    @Autowired
    TalentTrainingProgramInfoService talentTrainingProgramInfoService;
    /**
     * 去人才培养方案界面
     * @return String
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("tolist")
    @CheckFormat
    public String toViewTalentTrainingProgramInfos(){
        return "talent_training_program/talent_training_program_list";
    }

    /**
     * 访问人才培养方案详情界面
     *
     * @param talentTrainingProgramInfo 人才培养方案信息
     * @return ModelAndView 页面模板
     * @create on 2019/3/21 by winkyle
     */
    @RequestMapping(value="/view_talent_training_program")
    @CheckFormat
    ModelAndView viewTalentTrainingProgram(TalentTrainingProgramInfo talentTrainingProgramInfo){
        log.debug("========================viewTalentTrainingProgram{}",talentTrainingProgramInfo.toString());
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("talentTrainingProgramInfo", talentTrainingProgramInfo);
        modelAndView.setViewName("course/talent_training_program_detail");
        return modelAndView;
    }
    /**
     * 去添加人才培养界面
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("toAdd")
    public ModelAndView toAddTalentTrainingProgramInfo(){
        ModelAndView mv =this.getModelAndView();
        mv.addObject("dotype","add");
        mv.setViewName("talent_training_program/modify_talent_training_program");
        return mv;
    }
    /**
     * 去修改人才培养方案界面
     *
     * @param talentTrainingProgramId 人才培养方案Id
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("toEdit")
    public ModelAndView toEditTalentTrainingProgramInfo(String talentTrainingProgramId,HttpSession session){
        //System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%");
        ModelAndView mv =this.getModelAndView();
        TalentTrainingProgramInfo talentTrainingProgramInfo =new TalentTrainingProgramInfo();
        List<TalentTrainingProgramInfo> talentTrainingProgramInfoList =null;
        talentTrainingProgramInfoList = (List<TalentTrainingProgramInfo>)session.getAttribute("talentTrainingProgramInfoList");
        for(TalentTrainingProgramInfo item:talentTrainingProgramInfoList){
            if(talentTrainingProgramId.equals(item.getTalentTrainingProgramId())){
                talentTrainingProgramInfo =item;
            }
        }
        session.setAttribute("oldTalentTrainingProgramInfo",talentTrainingProgramInfo);
        mv.addObject("talentTrainingProgramInfo",talentTrainingProgramInfo);
//        mv.addObject("talentTrainingProgramId",talentTrainingProgramId);
        mv.addObject("dotype","edit");
        mv.setViewName("talent_training_program/modify_talent_training_program");
        return mv;
    }
    /**
     * 添加人才培养方案
     *
     * @param multipartFile 文件
     * @param awardYear 年份
     * @return Object 添加信息
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("add")
    @ResponseBody
    public Object addTalentTrainingProgramInfo(@RequestParam("file")MultipartFile multipartFile,@DateTimeFormat( pattern = "yyyy") Date awardYear) throws Exception{
        System.out.println("************");
        boolean result = talentTrainingProgramInfoService.saveTalentTrainingProgramInfo(multipartFile,awardYear);
        System.out.println("************"+result);
        if(!result){
            throw new InsertException("添加人才培养方案失败");
        }
        return ResultUtil.success("添加人才培养方案成功");
    }
    /**
     * 修改人才培养方案
     *
     * @param multipartFile 文件
     * @param awardYear 年份
     * @return Object 修改信息
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("edit")
    @ResponseBody
    public Object editTalentTrainingProgramInfo(@RequestParam("file")MultipartFile multipartFile,@DateTimeFormat( pattern = "yyyy") Date awardYear,HttpSession session) throws Exception{
        TalentTrainingProgramInfo talentTrainingProgramInfo =(TalentTrainingProgramInfo) session.getAttribute("oldTalentTrainingProgramInfo");
        System.out.println("@@@@@@@@@@@@@@@@@@@@@talentTrainingProgramInfo"+talentTrainingProgramInfo);
        boolean result = talentTrainingProgramInfoService.modifyTalentTrainingProgramInfo(talentTrainingProgramInfo,multipartFile,awardYear);
        if(!result){
            throw new UpdateException("修改人才培养方案失败");
        }
        return ResultUtil.success("修改人才培养方案成功");
    }

    /**
     * 查询所有的人才培养方案
     * @return List<TalentTrainingProgramInfo> 多个人才培养方案
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchTalentTrainingProgramInfos(Page page, HttpSession session) throws Exception{
        List<TalentTrainingProgramInfo> talentTrainingProgramInfoList =null;
        talentTrainingProgramInfoList = talentTrainingProgramInfoService.listTalentTrainingProgramInfos(page);
        session.setAttribute("talentTrainingProgramInfoList",talentTrainingProgramInfoList);
        return page.getDataTabelJson(talentTrainingProgramInfoList, DateEnum.ACCURATE_TO_YEAR);
    }

    @RequestMapping("drop")
    @ResponseBody
    public Object dropTalentTrainingProgramInfo(String talentTrainingProgramId,String oldFilePath) throws Exception{
        boolean result =talentTrainingProgramInfoService.removeTalentTrainingProgramInfo(talentTrainingProgramId,oldFilePath);
        if(!result){
            throw new DeleteException("删除人才培养方案失败");
        }
        return ResultUtil.success("删除人才培养方案成功");
    }
}
